package com.douwong.student.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.douwong.student.R;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.model.GroupFile;
import com.douwong.student.utils.ToastUtils;
import com.lidroid.xutils.bitmap.core.BitmapDecoder;
import com.lidroid.xutils.bitmap.core.BitmapSize;

import java.io.File;
import java.util.List;

/**
 * @autor yichao
 * @time 2017/4/1713:40
 * @des 小组讨论答案
 */
public  class ImageAdapter extends BaseAdapter {
    private Context mContext;
    List<GroupFile> resultList;
    private boolean isCommit =false;
    private boolean isEnd=false;

    public void setCommit(boolean commit) {
        isCommit = commit;
    }

    public void setEnd(boolean end) {
        isEnd = end;
    }

    public ImageAdapter(Context context, List<GroupFile> resultList) {
        mContext = context;
        this.resultList = resultList;
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.group_img, null);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.select_img);
        ImageView delete = (ImageView) convertView.findViewById(R.id.delete_img);
        try {
            if (resultList.get(position).fileName.equals("add")) {
                delete.setVisibility(View.INVISIBLE);
            } else {
                if (resultList.get(position).fileType == 1) {
                    File file = new File(resultList.get(position).filePath);
                    Bitmap bitmap = BitmapDecoder.decodeSampledBitmapFromFile(file.getAbsolutePath(),
                            new BitmapSize(100, 100), Bitmap.Config.RGB_565);
                    if (bitmap != null) {
                        imageView.setImageBitmap(bitmap);
                    }
                } else if (resultList.get(position).fileType == 2) {
                    imageView.setImageResource(R.mipmap.voice_3);
                }
            }
            delete.setOnClickListener(new View.OnClickListener() {// 删除
                @Override
                public void onClick(View v) {
                    if (isCommit || isEnd ) {
                        ToastUtils.makeText(mContext,"讨论结果已经提交，无法删除！");

                    } else {
                        if (BaseApplication.isLoginClass) {
                          //  deleteResult(resultList.get(position).fileName);
                            resultList.remove(position);
                            if(!resultList.get(resultList.size()-1).fileName.equals("add")){
                                GroupFile discussResult = new GroupFile();
                                discussResult.fileName = "add";
                                resultList.add(discussResult);
                            }
                            notifyDataSetChanged();
                        } else {
                            ToastUtils.makeText(mContext,"请先连接教师端！");
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }
}

